package com.alstudio.ui.module.main;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alstudio.R;
import com.alstudio.app.LocalEnv;
import com.alstudio.net.protocol.xmpp.data.XmppData;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.android.ALWifiManager.WifiStateChangedCallback;
import com.alstudio.utils.android.AndroidUtils.AirplaneModeChangedCallback;
import com.alstudio.utils.android.net.ALConnectionManager.ALConnectionType;
import com.alstudio.utils.log.Mylog;

public class MainActivity extends BaseActivity implements OnClickListener,
		WifiStateChangedCallback, AirplaneModeChangedCallback {

	private final String TAG = "MainActivity";
	private Button login;
	private EditText ip, port,user,pwd;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, TAG);
		initData();

	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setContentRes(R.layout.main_activity);
		login = (Button) findViewById(R.id.login);
		ip = (EditText) findViewById(R.id.ip);
		port = (EditText) findViewById(R.id.port);
		user = (EditText) findViewById(R.id.user);
		pwd = (EditText) findViewById(R.id.pwd);
		setViewOnclickListener(login, this);
		setViewOnclickListener(R.id.default_login, this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}


	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		setButtonText();
	}

	private void setButtonText() {
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.login:
			doLogin();
			break;
		case R.id.default_login:
			doDefaultLogin();
			break;
		}
	}

	@Override
	public void onWifiStateChangedCallback(boolean state) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDataConnectionEstablished(ALConnectionType type) {
		// TODO Auto-generated method stub
		super.onDataConnectionEstablished(type);
		if (type == ALConnectionType.TYPE_WIFI) {
		}
	}

	@Override
	public void onDataConnectionLost() {
		// TODO Auto-generated method stub
		super.onDataConnectionLost();
		Mylog.e("丢失丢失丢失丢失");
	}


	@Override
	public void onAirplaneModeChanged(boolean state) {
		
	}

	
	private void doLogin(){
		String ipAddr = ip.getText().toString().trim();
		String portAddr = port.getText().toString().trim();
		String username = user.getText().toString().trim();
		String passwd = pwd.getText().toString().trim();
		if(TextUtils.isEmpty(ipAddr)){
			Toast.makeText(this, "你没填ip地址", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(TextUtils.isEmpty(portAddr)){
			Toast.makeText(this, "你没填端口", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(TextUtils.isEmpty(username)){
			Toast.makeText(this, "你没填账号", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(TextUtils.isEmpty(passwd)){
			Toast.makeText(this, "你没填密码", Toast.LENGTH_SHORT).show();
			return;
		}
		
		XmppData data = new XmppData(ipAddr, portAddr, "ai", 50);
		data.setUsername(username);
		data.setPassword(passwd);
		
		LocalEnv.adapter.xmppLogin(data);
		
	}
	
	private void doDefaultLogin(){
		XmppData data = new XmppData("112.124.53.184", "5222", "ai", 50);
		data.setUsername("235517");
		data.setPassword("123456");
		
//		data = new XmppData("116.254.203.117", "5555", "mk", 50);
//		data.setUsername("200278");
//		data.setPassword("pppppp");
		
		LocalEnv.adapter.xmppLogin(data);
	}

}
